//
//  RadioPlayerViewController.m
//  HuLaQuan
//
//  Created by hok on 1/18/16.
//  Copyright © 2016 Jianghao. All rights reserved.
//

#import "RadioPlayerViewController.h"
#import "FMProgrameListenEngine.h"
#import "SHBAVPlayerView.h"

#import "AudioPlayer.h"

@interface RadioPlayerViewController ()<NSURLConnectionDataDelegate>
{
    AudioPlayer *_player;
}
@property (weak, nonatomic) IBOutlet UILabel *radioContent;
@property (weak, nonatomic) IBOutlet UIButton *playButton;

//player
@property(nonatomic,strong)SHBAVPlayerView *avView;

//logic model
@property(nonatomic,strong)FMProgrameListenEngine *programeListenEngine;

//dataSource
@property(nonatomic,strong)NSMutableArray *getProgrameDataSource;

@property(nonatomic,strong)NSDictionary *getProgrameListDic;

//programeIDUrl
@property(nonatomic,strong)NSString *programeKurl;

@end

@implementation RadioPlayerViewController

-(NSMutableArray *)getProgrameDataSource
{
    if (!_getProgrameDataSource) {
        _getProgrameDataSource = [[NSMutableArray alloc]init];
    }
    return _getProgrameDataSource;
}
-(NSMutableArray *)getSavePath
{
    if (!_getSavePath) {
        _getSavePath = [[NSMutableArray alloc]init];
    }
    return _getSavePath;
}
-(FMProgrameListenEngine *)programeListenEngine
{
    if (!_programeListenEngine) {
        _programeListenEngine = [[FMProgrameListenEngine alloc]init];
    }
    return _programeListenEngine;
}

- (void)setupUI
{
    //self.fromDownloadController = NO;
    
    self.title = @"节目收听";
    
    [self setupNavBackButton];
     _player = [AudioPlayer shareSingleton];
    
    self.view.backgroundColor = UIColorFromRGBA(0x222535, 1.0);
    
    UIImage* img1=[[UIImage imageNamed:@"952.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem*barItem1 = [[UIBarButtonItem alloc]
                                initWithImage:img1
                                style:UIBarButtonItemStylePlain
                                target:self
                                action:nil];
    self.navigationItem.rightBarButtonItem = barItem1;

}

- (IBAction)downloadSounds:(id)sender
{
    
}
- (IBAction)subscribe:(id)sender
{
    
}
- (IBAction)traficCondition:(id)sender
{
    
}
- (IBAction)interactiveWithMC:(id)sender
{
    
}
//播放按钮触发事件
-(void)updateUI
{
    [self.playButton setImage:[UIImage imageNamed:@"pause_radio"] forState:UIControlStateNormal];
    [_avView pause];
}
- (void)viewDidLoad {
    [super viewDidLoad];
}


- (void)getProgrammeListFinish:(NSNotification*) notification
{
    [YuNetworkStatusUtilities dismissHUD];
    NSDictionary *messageDic = [notification object];//获取到传递的对象
    if ([messageDic[@"isSuccess"] boolValue]) {
        NSLog(@"~~~~~~~reback%@",messageDic);
        
        if ([messageDic[@"result"] isKindOfClass:[NSArray class]])
        {
            [self.getProgrameDataSource addObjectsFromArray:messageDic[@"result"]];

        }
        else
        {
            [self.getProgrameDataSource addObject:messageDic[@"result"]];
        }
        
        self.getProgrameListDic = [self.getProgrameDataSource objectAtIndex:0];
        NSLog(@"!!!!!!!%@",self.getProgrameListDic);
        
    }
    else{
        
        [self showNetWorkError:messageDic[@"message"]];
        
    }
    
}
- (void)appointmentFinish:(NSNotification*) notification
{
   
    [YuNetworkStatusUtilities dismissHUD];
    NSDictionary *messageDic = [notification object];//获取到传递的对象
    if ([messageDic[@"isSuccess"] boolValue]) {
        // NSLog(@"~~~~~~~Back%@",messageDic);
    }
    else{
        [self showNetWorkError:messageDic[@"message"]];
        
    }
    
}
- (void)appointmentListFinish:(NSNotification*) notification
{
  
    [YuNetworkStatusUtilities dismissHUD];
    NSDictionary *messageDic = [notification object];//获取到传递的对象
    if ([messageDic[@"isSuccess"] boolValue]) {
    }else{
        [self showNetWorkError:messageDic[@"message"]];
        
    }
    
}
- (void)programmeListenFinish:(NSNotification*) notification
{
  
    [YuNetworkStatusUtilities dismissHUD];
    NSDictionary *messageDic = [notification object];//获取到传递的对象
    if ([messageDic[@"isSuccess"] boolValue]) {
        NSLog(@"Back%@",messageDic);
        
        self.programeKurl = [[messageDic objectForKey:@"result"]objectForKey:@"channelUrl"];
        
        if (!_avView) {
            _avView = [[SHBAVPlayerView alloc] initWithFrame:CGRectMake(30, 100, 50, 50) url:self.programeKurl];
        }
        
        [self.avView play];
    }
    else{
        [self showNetWorkError:messageDic[@"message"]];
        
    }
    
}
#pragma mark - Life Cycle
//数据处理
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getProgrammeListFinish:) name:@"GetProgrammeListFinish" object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(appointmentFinish:) name:@"AppointmentFinish" object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(appointmentListFinish:) name:@"AppointmentListFinish" object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(programmeListenFinish:) name:@"ProgrammeListenFinish" object:nil];
    //
    [YuNetworkStatusUtilities showProgressMessage:NETWORK_LOADING];
    [self.programeListenEngine getProgrammeList];
    
    //[self.programeListenEngine appointmentList];
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"GetProgrammeListenFinish" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"AppointmentFinish" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"AppointmentListFinish" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ProgrammeListenFinish" object:nil];
    
    [self.avView removeFromSuperview];
    self.avView = nil;
    
}

#pragma mark -Button Event
- (IBAction)tapPlayButton:(id)sender {
    
//    NSUserDefaults *getPath = [NSUserDefaults standardUserDefaults];
//    _getSavePath = [getPath objectForKey:@"savePath"];
//     NSLog(@"!!!!!!!!save%@",_getSavePath);
//    if (_getSavePath) {
//        
//       self.fromDownloadController = YES;
//    }
    //本地播放
    if (self.fromDownloadController) {
        
        if ([[self.playButton imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"play_radio"]])
        {
            [self.playButton setImage:[UIImage imageNamed:@"pause_radio"] forState:UIControlStateNormal];
//            for (NSString *odj in _getSavePath) {
            NSString *path = [[NSBundle mainBundle] pathForResource:@"La La La" ofType:@"mp3"];
              [_player startPlayWithURL:[self.getSavePath firstObject]];
//            }
        }
        else
        {
            [self.playButton setImage:[UIImage imageNamed:@"play_radio"] forState:UIControlStateNormal];
            [_player pausePlay];
        }
        //remove
//        [getPath removeObjectForKey:@"savePath"];
    }
    else
    {
        //网络播放
        if ([[self.playButton imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"play_radio"]]) {
        
        [YuNetworkStatusUtilities showProgressMessage:NETWORK_LOADING];
        [self.playButton setImage:[UIImage imageNamed:@"pause_radio"] forState:UIControlStateNormal];
        [self.programeListenEngine programemListen:[self.getProgrameListDic objectForKey:@"programmeID"] andChannelName:[self.getProgrameListDic objectForKey:@"channelName"]];
   
        }
        else{
        [self.playButton setImage:[UIImage imageNamed:@"play_radio"] forState:UIControlStateNormal];
        [self.avView pause];
     }
    
  }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

